package com.jcgyl.export.service.Impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jcgyl.export.dao.PostDao;
import com.jcgyl.export.dao.RoleDao;
import com.jcgyl.export.dao.TbsysmenuDao;
import com.jcgyl.export.dao.TbsysuserDao;
import com.jcgyl.export.dao.TbviewsysuserDao;
import com.jcgyl.export.entity.Post;
import com.jcgyl.export.entity.Role;
import com.jcgyl.export.entity.Tbsysmenu;
import com.jcgyl.export.entity.Tbsysuser;
import com.jcgyl.export.service.UserInfoService;
import com.jcgyl.export.util.PasswordHelper;
import com.jcgyl.export.util.PasswordSalt;
@Service
public class UserInfoServiceImpl implements UserInfoService{

	// 用户持久层注入
	@Autowired
	private TbsysuserDao tbsysuserDao;
	//用户视图持久层注入
	@Autowired
	private TbviewsysuserDao tbviewsysuserDao;
	//岗位持久层注入
	@Autowired
	private PostDao postDao;
	//角色持久层注入
	@Autowired
	private RoleDao roleDao;
	// 菜单持久层注入
	@Autowired
	private TbsysmenuDao tbsysmenuDao;
	@Override
	public List<Map<String, Object>> findObjects() {
		
		return tbsysuserDao.findObjects() ;
	}

	@Override
	public Integer getRowCount() {
		int row = tbsysuserDao.getRowCount();
		return row;
	}
	/**
	 * 添加用户
	 * JL
	 */
	@Override
	public Integer saveObject(Tbsysuser tbsysuser) {
		if(tbsysuserDao.getNewId() !=null) {
			tbsysuser.setUid(tbsysuserDao.getNewId()+1);
		}else {
			tbsysuser.setUid(1L);
		}
		String salt = PasswordSalt.getRandomString(32);
		tbsysuser.setSalt(salt);
		tbsysuser.setValid(1);
		String password =PasswordHelper.encryptPassword("123456", salt);//默认123456OK
		tbsysuser.setPassword(password);
		tbsysuser.setCreateTime(new Date());
		int row = tbsysuserDao.insert(tbsysuser);
		return row;
	}
	/**
	 * 根据id删除用户
	 * JL
	 */
	@Override
	public Integer deleteObject(Long uid) {
		int row = tbsysuserDao.deleteByPrimaryKey(uid);
		return row;
	}
	/**
	 * 根据条件查询数据
	 * JL
	 */
	@Override
	public List<Map<String,Object>> findObject(String departmentname, String name, Integer pageSize, Integer pageCurrent) {
		int startIndex=(pageCurrent-1)*pageSize;
		List<Map<String,Object>>list = tbviewsysuserDao.findObject(departmentname, name, startIndex, pageSize);
		return list;
	}
	/**
	 * 根据条件查询条数
	 * JL
	 */
	@Override
	public Integer getRow(String departmentname, String name) {
		int row = tbviewsysuserDao.getRow(departmentname, name);
		return row;
	}
	/**
	 * 修改用户信息
	 * JL
	 */
	@Override
	public Integer updataObject(Tbsysuser tbsysuser) {
		tbsysuser.setModifiedTime(new Date());
		int row = tbsysuserDao.updateByPrimaryKey(tbsysuser);
		return row;
	}

	@Override
	public Integer findName(String uname) {
		int row =tbsysuserDao.findByName(uname);
		return row;
	}

	@Override
	public Integer findNameByPw(String uname,String password) {
		int row;
		int unamerow = tbsysuserDao.findByName(uname);
		if(unamerow == 1) {
			
			String pw = tbsysuserDao.findNameByPw(uname);
			if(pw.equals(password)) {
				row = 1;
				return row;
			}
			if(pw.equals("")) {
				row = 2;
				return row;
			}
		}
		return null;
	}



	@Override
	public String findPwById(Long id) {
		String pw =tbsysuserDao.findPwById(id);
		return pw;
	}

	@Override
	public Tbsysuser findByUserName(String uname) {
		
		return tbsysuserDao.findByUserName(uname);
	}

	@Override
	public Integer resetpw(Long id) {
		String salt = PasswordSalt.getRandomString(32);
		String password =PasswordHelper.encryptPassword("123456", salt);
		int row =tbsysuserDao.resetpw(id, password,salt);
		return row;
	}

	

	

	/**
	 * 根据用户名查询岗位信息
	 * @param uname 用户名
	 * @return
	 * JL
	 */
	@Override
	public Post findPostByUserName(String uname) {
		return postDao.findPostByUserName(uname);
	}
	/**
	 * 根据岗位id查询角色信息
	 * JL
	 */
	@Override
	public List<Role> findRoleByPostId(int id) {
		return roleDao.findRoleByPostId(id);
	}
	/**
	 * 根据角色表id查询角色的权限
	 * 
	 * JL
	 */
	@Override
	public List<Tbsysmenu> findMenuByRoleId(int id) {
		return tbsysmenuDao.findMenuByRoleId(id);
	}

	
}
